package com.itheima.service;

import com.itheima.common.R;
import com.itheima.domain.Address;
import com.itheima.domain.OrderDetail;
import com.itheima.domain.OrderDto;
import com.itheima.domain.ShoppingCart;
import com.itheima.mapper1.AddressMapper;
import com.itheima.mapper1.OrderDetailMapper;
import com.itheima.mapper1.OrdersMapper;
import com.itheima.mapper1.ShoppingCartMapper;
import com.itheima.pojo.Order;
import com.itheima.pojo.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * @author wuyiqiang
 * @2022/6/19 19:47
 */
@Service
public class OrderService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 提交订单
     *
     * @param order
     */
    public void submit(Order order) {
        //通过当前地址id查询当前地址信息
        Address address = addressMapper.selectById(order.getAddressBookId());
        //设置地址信息
        order.setPhone(address.getPhone());
        order.setAddress(address.getDetail());
        order.setConsignee(address.getConsignee());
        order.setUsername(address.getConsignee());
        //定义一个总额的自变量
        BigDecimal totalAmount = new BigDecimal(0);
        //根据当前用户id获取购物车信息
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByUserId(order.getUserId());
        //读取购物车中的信息并写入订单批量详情中
        //创建一个订单详情集合存储购物车的数据
        List<OrderDetail> orderDetailList = new ArrayList<>();
        //遍历购物车数据并写入订单详情中
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(UUID.randomUUID().toString());//编号
            orderDetail.setOrderId(order.getId());//订单号
            orderDetail.setDishId(shoppingCart.getDishId());//菜品Id
            orderDetail.setSetmealId(shoppingCart.getSetmealId());//套餐id
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());//菜品口味
            orderDetail.setNumber(shoppingCart.getNumber());//数量
            orderDetail.setAmount(shoppingCart.getAmount());//金额
            orderDetail.setImage(shoppingCart.getImage());//图片
            orderDetail.setName(shoppingCart.getName());//菜品名
            //计算当前菜品总额
            BigDecimal multiply = shoppingCart.getAmount().multiply(BigDecimal.valueOf(shoppingCart.getNumber()));
            //总额增加
            totalAmount = totalAmount.add(multiply);
            //集合增加数据
            orderDetailList.add(orderDetail);
        }
        //设置订单总额
        order.setAmount(totalAmount);
        //保存订单
        ordersMapper.submit(order);
        //保存订单详情

        orderDetailMapper.save(orderDetailList);
        //清空购物车
        shoppingCartMapper.delete(order.getUserId());

    }

    /**
     * 查询订单
     *
     * @param page
     * @param pageSize
     * @return
     */
    public R<Page> page(Integer page, Integer pageSize) {
        page = (page - 1) * pageSize;
        //查询订单列表
        List<Order> orderList = ordersMapper.page(page, pageSize);

        Page page2 = new Page();

        //创建orderDto的orderDetail集合
        List<OrderDto> orderDetailList = orderList.stream().map(order -> {
            OrderDto orderDto = new OrderDto();
            //copy数据信息
            BeanUtils.copyProperties(order, orderDto);
            //通过orderId获取订单详情
            List<OrderDetail> orderDetailList1 = orderDetailMapper.selectByOrderId(orderDto.getId());
            orderDto.setOrderDetailList(orderDetailList1);
            return orderDto;
        }).collect(Collectors.toList());

        //设置返回页的总条数
        page2.setTotal(ordersMapper.selectAll());
        //设置返回页的每页数量
        page2.setSize(pageSize);
        if (orderDetailList != null) {
            //设置返回页返回类对象的当前页
            page2.setCurrent(page);
            //将获得到的数据写进返回类中
            page2.setRecords(orderDetailList);
            return R.success(page2);
        } else {
            return R.error("没数据了");
        }

    }
}
